# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>

_pkgname='intel-oneapi-compiler-shared'
pkgbase="${_pkgname}-runtime"
pkgname=("${_pkgname}-runtime" "${_pkgname}-runtime-libs" "${_pkgname}-opencl-cpu")
pkgver=2023.0.0
_debpkgrel=25370
pkgrel=7
_pkgdesc="Intel oneAPI compiler runtime libraries"
arch=('loong64' 'x86_64')
url='https://software.intel.com/content/www/us/en/develop/tools/oneapi.html'
license=("custom")
depends=('level-zero-loader')
source=("https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${_pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb")
b2sums=('f75dd1a84724dd2fc0328ef99fae04a4095db77698e4c62d10f0b696f99909d33633ebc740f7fb1f6564c60399fde2728ef0e0f2e694388b24155e86be4e69fe'
        'a8deb7510bd8aed85e6e72b162f22f866e3b2f9f1ad3da60ed385d9ad86d6460e450cad1f047c1d84cf00763bd1c2fdfe2fc9f19033858a0bfbe6ed9005db45a')

noextract=(
    "${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    "${_pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
)

prepare() {
    mkdir "${_pkgname}-${pkgver}"
    ar x "${srcdir}/${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    tar xvf data.tar.xz -C "${_pkgname}-${pkgver}"
    rm data.tar.xz
    ar x "${srcdir}/${_pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
    tar xvf data.tar.xz -C "${_pkgname}-${pkgver}"
    rm data.tar.xz
}

package_intel-oneapi-compiler-shared-runtime-libs() {
    depends=('intel-oneapi-common')
    conflicts=('intel-oneapi-basekit')
    pkgdesc="${_pkgdesc}: Minimal compiler libraries"

    local _lib_path='/opt/intel/oneapi/compiler'
    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/compiler"
    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/compiler/include" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux/compiler"

    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/compiler/lib" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux/compiler"

    # add latest and common symlink
    ln -s "${_lib_path}/${pkgver}" "${pkgdir}/${_lib_path}/latest"
    ln -s "${_lib_path}/latest/linux/compiler/lib/intel64_lin" \
          "${pkgdir}/${_lib_path}/${pkgver}/linux/compiler/lib/intel64"

    # allow libs to be found
    local _ldso_conf="${pkgdir}"/etc/ld.so.conf.d
    install -d "${_ldso_conf}"
    echo "${_lib_path}/latest/linux/compiler/lib/intel64_lin" >> "${_ldso_conf}/${pkgname}.conf"

    install -d "${pkgdir}"/usr/share/licenses/"${pkgname}"
    ln -s /usr/share/licenses/intel-oneapi "${pkgdir}"/usr/share/licenses/"${pkgname}"/oneapi
}

package_intel-oneapi-compiler-shared-opencl-cpu() {
    depends=('intel-oneapi-openmp' 'intel-oneapi-compiler-shared-runtime-libs'
        'opencl-headers' 'opencl-icd-loader') 
    conflicts=('intel-oneapi-basekit')
    pkgdesc="${_pkgdesc}: OpenCL CPU runtime"

    local _lib_path='/opt/intel/oneapi/compiler'
    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/bin"
    local _bin
    for _bin in sycl-ls sycl-post-link; do
        cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/bin/${_bin}" \
            "${pkgdir}/${_lib_path}/${pkgver}/linux/bin"
    done

    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/lib"
    local _lib
    cd "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/lib"
    for _lib in libOpenCL.so* libpi_esimd_emulator.so libpi_opencl.so; do
        cp -a "${_lib}" "${pkgdir}/${_lib_path}/${pkgver}/linux/lib"
    done

    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/lib/x64"
    cd "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/lib/x64"
    for _lib in __ocl_svml* cl*.rtl cllibrary*.o libOclCpuBackEnd* libcl_logger* libcpu_device* libintelocl*; do
        cp -a "${_lib}" "${pkgdir}/${_lib_path}/${pkgver}/linux/lib/x64"
    done

    # allow libs to be found
    local _ldso_conf="${pkgdir}"/etc/ld.so.conf.d
    install -d "${_ldso_conf}"
    echo "${_lib_path}/latest/linux/lib" > "${_ldso_conf}/${pkgname}.conf"
    echo "${_lib_path}/latest/linux/lib/x64" >> "${_ldso_conf}/${pkgname}.conf"

    echo "/opt/intel/oneapi/compiler/${pkgver}/linux/lib/libOpenCL.so" > "${pkgname}.icd"
    install -Dm644 "${pkgname}.icd" "$pkgdir/etc/OpenCL/vendors/${pkgname}.icd"

    install -d "${pkgdir}"/usr/share/licenses/"${pkgname}"
    ln -s /usr/share/licenses/intel-oneapi "${pkgdir}"/usr/share/licenses/"${pkgname}"/oneapi
}

package_intel-oneapi-compiler-shared-runtime() {
    depends=('intel-oneapi-openmp' 'intel-oneapi-common'
        'intel-oneapi-compiler-shared-runtime-libs' 'intel-oneapi-compiler-shared-opencl-cpu')
    provides=("${_pkgname}-common")
    conflicts=('intel-oneapi-basekit')
    pkgdesc="${_pkgdesc}: Shared runtime without OpenCL"

    local _lib_path='/opt/intel/oneapi/compiler'
    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux"
    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/bin-llvm" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux"

    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/include" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux"

    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/bin"
    local _bin
    for _bin in aocl-ioc64 icx-cc icx-cl icx.cfg ioc64; do
        cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/bin/${_bin}" \
            "${pkgdir}/${_lib_path}/${pkgver}/linux/bin"
    done

    local _lib
    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/lib"
    cd "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/lib"
    for _lib in clbltfnshared.rtl icx-lto.so libcm.so libigfxcmrt_emu.so libshim.so libshim_l0.so libxpti.a libxptifw.so libsycl-*; do
        cp -a "${_lib}" "${pkgdir}/${_lib_path}/${pkgver}/linux/lib"
    done

    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/lib/x64"
    cd "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/lib/x64"
    for _lib in libclang_compiler* libcommon_clang* libtask_executor*; do
        cp -a "${_lib}" "${pkgdir}/${_lib_path}/${pkgver}/linux/lib/x64"
    done
    
    # allow libs to be found
    local _ldso_conf="${pkgdir}"/etc/ld.so.conf.d
    install -d "${_ldso_conf}"
    echo "${_lib_path}/latest/linux/lib" > "${_ldso_conf}/${pkgname}.conf"
    echo "${_lib_path}/latest/linux/lib/x64" >> "${_ldso_conf}/${pkgname}.conf"

    install -d "${pkgdir}"/usr/share/licenses/"${pkgname}"
    ln -s /usr/share/licenses/intel-oneapi "${pkgdir}"/usr/share/licenses/"${pkgname}"/oneapi
}

